#!/usr/bin/env python
# encoding: utf-8
# Date: 2017-06-19 09:07:37
# file:views.py
# Email: wang521kang@outlook.com
# Author: 王康
from address.models import Address
from utils.views import request_parms_check, get_json


@request_parms_check(name=('收货人', 'r'), phone=('电话', 'r,int'), add_province=('省', 'r'),
                     add_city=('市', 'r'), add_district=('区', 'r'), add_detail=('详细地址', 'r'))
def create_address(request, name, phone, add_province, add_city, add_district, add_detail):
    user_id = request.user.id
    address = Address()
    address.user_id = user_id
    address.name = name
    address.phone = phone
    address.add_province = add_province
    address.add_city = add_city
    address.add_district = add_district
    address.add_detail = add_detail
    address_num = Address.objects.filter(user_id=user_id).count()
    if address_num == 0:
        address.is_default = True
    address.save()
    return get_json(True, '地址创建成功')


@request_parms_check()
def query_address(request):
    user_id = request.user.id
    query = Address.objects.filter(user_id=user_id, is_active=True).all()
    l = []
    for address in query:
        res = {}
        res['id'] = address.id
        res['name'] = address.name
        res['phone'] = address.phone
        res['add_province'] = address.add_province
        res['add_city'] = address.add_city
        res['add_district'] = address.add_district
        res['add_detail'] = address.add_detail
        res['is_default'] = address.is_default
        l.append(res)
    return get_json(True, '', l)


@request_parms_check(address_id=('地址id', 'r,int'))
def get_address(request, address_id):
    address = Address.objects.get(id=address_id)
    res = {}
    res['id'] = address.id
    res['name'] = address.name
    res['phone'] = address.phone
    res['add_province'] = address.add_province
    res['add_city'] = address.add_city
    res['add_district'] = address.add_district
    res['add_detail'] = address.add_detail
    res['is_default'] = address.is_default
    return get_json(True, '', res)


@request_parms_check()
def get_address_default(request):
    user_id = request.user.id
    add_num = Address.objects.filter(is_active=True, is_default=True, user_id=user_id).count()
    if add_num == 1:
        address = Address.objects.get(is_default=True, user_id=user_id)
        res = {}
        res['id'] = address.id
        res['name'] = address.name
        res['phone'] = address.phone
        res['add_province'] = address.add_province
        res['add_city'] = address.add_city
        res['add_district'] = address.add_district
        res['add_detail'] = address.add_detail
        return get_json(True, '', res)


@request_parms_check(address_id=('地址id', 'r,int'))
def address_is_default(request, address_id):
    adds = Address.objects.filter(is_default=True).all()
    for item in adds:
        item.is_default = False
        item.save()
    address = Address.objects.get(id=address_id)
    address.is_default = True
    address.save()
    return get_json(True, '已设为默认')


@request_parms_check(address_id=('地址id', 'r,int'))
def address_delete(request, address_id):
    address = Address.objects.get(id=address_id)
    address.is_active = False
    address.save()
    return get_json(True, '地址已删除')


@request_parms_check(address_id=('地址id', 'r,int'), name=('收货人', 'r'), phone=('电话', 'r,int'), add_province=('省', 'r'),
                     add_city=('市', 'r'), add_district=('区', 'r'), add_detail=('详细地址', 'r'),
                     add_is_default=('是否默认', 'r'))
def modify_address(request, address_id, name, phone, add_province, add_city, add_district, add_detail, add_is_default):
    address = Address.objects.get(id=address_id)
    address.name = name
    address.phone = phone
    address.add_province = add_province
    address.add_city = add_city
    address.add_district = add_district
    address.add_detail = add_detail
    address.is_default = add_is_default
    address.save()
    return get_json(True, '修改成功')
